﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace CoreLib.Message
{
    public abstract class MessageProcessor
    {
        private string log = "";//DEBUG

        private static Object s_lock = new Object();//用于lock块
        public virtual bool ExitUntilQueueCleared
        {
            get { return true; }
        }

        public MessageProcessor()
        {
            threadMessageQueue = new Thread(new ThreadStart(CheckMessageQueue));
            threadMessageQueue.Start();
        }

        protected bool killThreadFlag = false;
        /// <summary>
        /// 消息队列线程
        /// </summary>
        protected Thread threadMessageQueue;

        private Queue<ITracyMessage> _messageQueue = new Queue<ITracyMessage>();
        /// <summary>
        /// 消息队列
        /// </summary>
        protected virtual Queue<ITracyMessage> MessageQueue
        {
            get { return _messageQueue; }
        }

        /// <summary>
        /// 消息入队
        /// </summary>
        public virtual void EnqueueMessage(ITracyMessage msg)
        {
            lock (s_lock)
            {
                MessageQueue.Enqueue(msg);
                log += "Enqueued\n";
            }
            CallOnQueueModified(this, new TracyGeneralEventArgs(MessageQueue.Count));
        }

        protected virtual void CallOnMessageProcessed(object sender, TracyMessageEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected virtual void CallOnQueueModified(object sender, TracyGeneralEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected virtual void ProcessMessage(ITracyMessage msg)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// （独立线程）检查消息队列
        /// </summary>
        protected virtual void CheckMessageQueue()
        {
            bool queueChangeFlag = false;
            ITracyMessage msg;
            while (!killThreadFlag)
            {
                queueChangeFlag = false;
                while (MessageQueue.Count > 0 && (ExitUntilQueueCleared || !killThreadFlag))
                {
                    log += "FoundOne\n";
                    queueChangeFlag = true;
                    msg = null;
                    lock (s_lock)
                    {
                        if (MessageQueue.Count > 0)
                        {
                            msg = MessageQueue.Dequeue();
                            log += "Dequeued\n";
                        }
                    }

                    if (msg != null)
                    {
                        ProcessMessage(msg);
                        //TODO: 使用自定义的EventArgs
                        CallOnMessageProcessed(this, new TracyMessageEventArgs(msg));
                    }
                    else
                    {
                        throw new Exception("消息为空");
                    }
                }
                if (queueChangeFlag)
                {
                    CallOnQueueModified(this, new TracyGeneralEventArgs(MessageQueue.Count));
                }
                Thread.Sleep(10);
            }
        }
    }

    /// <summary>
    /// OnMessageProcessed事件的参数
    /// </summary>
    public class TracyMessageEventArgs : EventArgs
    {
        public TracyMessageEventArgs()
            : base()
        {

        }

        public TracyMessageEventArgs(ITracyMessage message)
            : base()
        {
            _message = message;
        }

        ITracyMessage _message = null;
        /// <summary>
        /// 原始消息
        /// </summary>
        public ITracyMessage Message
        {
            get { return _message; }
        }
    }

    /// <summary>
    /// 事件的通用参数
    /// </summary>
    public class TracyGeneralEventArgs : EventArgs
    {
        public TracyGeneralEventArgs()
            : base()
        {

        }

        public TracyGeneralEventArgs(object data)
            : base()
        {
            _data = data;
        }

        object _data = null;
        /// <summary>
        /// 数据
        /// </summary>
        public object Data
        {
            get { return _data; }
            set { _data = value; }
        }
    }
}
